package com.jjpro.intercept;
import jiujiu.Head.ApiHead;
import jiujiu.Verification.ApiVerification;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@Component
public class CheckSignInterceptor implements HandlerInterceptor {

    private final static Logger LOGGER = LoggerFactory.getLogger(CheckSignInterceptor.class);


    private final static String APP_KEY="402880496058fbb7016058fc201e0009";


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object
            handler) throws IOException {
        boolean flag = false;
        try{

            String result = VerificationHead(request);
            LOGGER.info("验证结果:" + result);
            JSONObject res = new JSONObject(result);
            String msg = res.get("msg").toString();
            if(!"success".equals(msg)){
                setResult(response,result);
            }else{
                flag = true;
            }
        }catch (Exception e){
            LOGGER.info("Exception",e);
        }
        return flag;
    }

    /**
     * 添加请求头
     * @param request
     * @return
     * @throws Exception
     */
    public static String VerificationHead(HttpServletRequest request)throws Exception{
        ApiHead apiHead = new ApiHead();
        apiHead.setVersion(request.getHeader("version"));
        apiHead.setCertType(request.getHeader("certType"));
        apiHead.setCertification(request.getHeader("certification"));
        apiHead.setTimestamp(request.getHeader("timestamp"));
        apiHead.setPlat(request.getHeader("plat"));
        apiHead.setIsTest(request.getIntHeader("isTest"));
        if (LOGGER.isDebugEnabled()){
            try {
                LOGGER.debug("Version:" + request.getHeader("version"));
                LOGGER.debug("CertType:" + request.getHeader("certType"));
                LOGGER.debug("Certification:" + request.getHeader("certification"));
                LOGGER.debug("Timestamp:" + request.getHeader("timestamp"));
                LOGGER.debug("Plat:" + request.getHeader("plat"));
                LOGGER.debug("IsTest:" + request.getHeader("isTest"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        String result = ApiVerification.VerificationHandle(apiHead,APP_KEY);
        return result;
    }

    private void setResult(HttpServletResponse response, String data) throws IOException {
        response.setHeader("Content-type","text/html;charset=UTF-8");
        OutputStream stream = response.getOutputStream();
        stream.write(data.getBytes("UTF-8"));
        stream.flush();
        stream.close();
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
